package logic

import (
	"cart_srv/model/mysql"
	"context"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListCartLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListCartLogic {
	return &ListCartLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListCartLogic) ListCart(in *cart.ListCartRequest) (*cart.ListCartResponse, error) {
	// todo: add your logic here and delete this line
	c := mysql.Cart{}
	list, err := c.List(in.UserId)

	if err != nil {
		logx.Error("展示失败", err)
		return nil, err
	}
	var cartList []*cart.CartItem
	for _, v := range list {
		cartList = append(cartList, &cart.CartItem{
			GoodsId:    v.GoodsId,
			GoodsTitle: v.GoodsTitle,
			GoodsPrice: float32(v.GoodsPrice),
			Num:        v.Num,
			UserId:     v.UserID,
			Checked:    v.Checked,
		})
	}
	return &cart.ListCartResponse{
		CartList: cartList,
	}, nil
}
